﻿Imports System.Collections.Generic
Imports System.Data
Imports System.Windows.Forms
Imports Microsoft.Reporting.WebForms
Partial Class Form_frmReportAmloCBS
    Inherits System.Web.UI.Page

    Dim strSql As String
    Dim strRptType As String
    Dim strDeptCode As String
    Dim strZoneCode As String
    Dim strBranchCode As String
    Dim strStartDate As String
    Dim strEndDate As String
    Dim UserRight As clsSessionMenuRight

    Protected Sub form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles form1.Load

        If Not (IsPostBack) Then
            hdfRPTTYPE.Value = Request.Item("RPTTYPE")
            hdfDEPTCODE.Value = Request.Item("DEPTCODE")
            hdfBRANCHCODE.Value = Request.Item("BRANCHCODE")
            hdfZONECODE.Value = Request.Item("ZONECODE")
            hdfSTARTDATE.Value = Request.Item("STARTDATE")
            hdfENDDATE.Value = Request.Item("ENDDATE")

            strRptType = Request.Item("RPTTYPE")
            strDeptCode = Request.Item("DEPTCODE")
            strZoneCode = Request.Item("ZONECODE")
            strBranchCode = Request.Item("BRANCHCODE")
            strStartDate = Request.Item("STARTDATE")
            strEndDate = Request.Item("ENDDATE")
        Else
            If Not (IsCallback) Then
                strRptType = convertStringEncode(hdfRPTTYPE.Value)
                strDeptCode = convertStringEncode(hdfDEPTCODE.Value)
                strZoneCode = convertStringEncode(hdfZONECODE.Value)
                strBranchCode = convertStringEncode(hdfBRANCHCODE.Value)
                strStartDate = convertStringEncode(hdfSTARTDATE.Value)
                strEndDate = convertStringEncode(hdfENDDATE.Value)
            Else
                strRptType = (hdfRPTTYPE.Value)
                strDeptCode = (hdfDEPTCODE.Value)
                strZoneCode = (hdfZONECODE.Value)
                strBranchCode = (hdfBRANCHCODE.Value)
                strStartDate = (hdfSTARTDATE.Value)
                strEndDate = (hdfENDDATE.Value)
            End If

        End If
        If (strRptType = "Amlo101MapCbs") Then
            printRptAmlo101_Map_CBS()
        ElseIf (strRptType = "Amlo101Cbs") Then
            printRptAmlo101_CBS()
        ElseIf (strRptType = "Amlo102MapCbs") Then
            printRptAmlo102_Map_CBS()
        End If
    End Sub

    Private Sub printRptAmlo101_Map_CBS()
        Dim ds As New dsAmloReport
        Dim cls As New clsConnToDbByReport
        Dim re As Amlo1_01_Map_CBS = New Amlo1_01_Map_CBS
        Dim colVarParameter As Collection = New Collection
        Dim strPrmCondition As String = ""
        Dim clsVarParameter As clsVariable
        Dim clsFormatDate As New clsDateFormat
        Dim strDateStart As String = ""
        Dim strDateEnd As String = ""
        UserRight = Session("clsMenuRight")

        strSql = "select * from V_AMLO101_CBS t where 1=1 "

        If Not (strDeptCode = "0") Then
            strSql = strSql & " and ( cbs_deptcode = :DEPTCODE or deptcode = :DEPTCODE)"
            clsVarParameter = New clsVariable() With {.nameColumn = "DEPTCODE", _
                                                      .valueColumn = strDeptCode}
            colVarParameter.Add(clsVarParameter)
        End If
        If Not (strZoneCode = "0") Then
            strSql = strSql & " and ( cbs_zonecode = :ZONECODE or zonecode = :ZONECODE)"
            clsVarParameter = New clsVariable() With {.nameColumn = "ZONECODE", _
                                                      .valueColumn = strZoneCode}
            colVarParameter.Add(clsVarParameter)
        End If
        If Not (strBranchCode = "0") Then
            strSql = strSql & " and ( cbs_branchcode = :BRANCHCODE or branchcode = :BRANCHCODE)"
            clsVarParameter = New clsVariable() With {.nameColumn = "BRANCHCODE", _
                                                      .valueColumn = strBranchCode}
            colVarParameter.Add(clsVarParameter)
        End If

        If Not (strStartDate = "" AndAlso strEndDate = "") Then
            Dim dteStartDate As Date = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, strStartDate) & " 00:00:00"
            Dim dteEndDate As Date = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, strEndDate) & " 23:59:59"
            strDateStart = dteStartDate.ToShortDateString
            strDateEnd = dteEndDate.ToShortDateString

            strSql = strSql & " and ((cbs_tran_date >= :STARTDATE and cbs_tran_date <= :ENDDATE)"
            strSql = strSql & " or (transdate >= :STARTDATE and transdate <= :ENDDATE))"

            clsVarParameter = New clsVariable() With {.nameColumn = "STARTDATE", _
                                                      .valueColumn = dteStartDate}
            colVarParameter.Add(clsVarParameter)

            clsVarParameter = New clsVariable() With {.nameColumn = "ENDDATE", _
                                                      .valueColumn = dteEndDate}
            colVarParameter.Add(clsVarParameter)
            If (strPrmCondition = "") Then
                strPrmCondition = strPrmCondition & String.Format(Resources.resPrintRptMaster.cdtDate, dteStartDate & "   ถึง   " & dteEndDate)
            Else
                strPrmCondition = strPrmCondition & "   ;   " & String.Format(Resources.resPrintRptMaster.cdtDate, dteStartDate & "   ถึง   " & dteEndDate)
            End If

        Else
            If (strPrmCondition = "") Then
                strPrmCondition = strPrmCondition & String.Format(Resources.resPrintRptMaster.cdtDate, "-")
            Else
                strPrmCondition = strPrmCondition & "   ;   " & String.Format(Resources.resPrintRptMaster.cdtDate, "-")
            End If
        End If

        cls.rptAMLO101_CBS(re, strSql, ds, colVarParameter)
        re.prmStartDate.Value = strDateStart
        re.prmEndDate.Value = strDateEnd

        ReportViewer1.Report = re



    End Sub

    Private Sub printRptAmlo101_CBS()
        Dim ds As New dsAmloReport
        Dim cls As New clsConnToDbByReport
        Dim re As Amlo1_01_CBS = New Amlo1_01_CBS
        Dim colVarParameter As Collection = New Collection
        Dim strPrmCondition As String = ""
        Dim clsVarParameter As clsVariable
        Dim clsFormatDate As New clsDateFormat
        Dim strDateStart As String = ""
        Dim strDateEnd As String = ""
        UserRight = Session("clsMenuRight")

        strSql = "select * from V_AMLO101_CBS t where cbs_seq is not null "

        If Not (strDeptCode = "0") Then
            strSql = strSql & " and ( cbs_deptcode = :DEPTCODE or deptcode = :DEPTCODE)"
            clsVarParameter = New clsVariable() With {.nameColumn = "DEPTCODE", _
                                                      .valueColumn = strDeptCode}
            colVarParameter.Add(clsVarParameter)
        End If
        If Not (strZoneCode = "0") Then
            strSql = strSql & " and ( cbs_zonecode = :ZONECODE or zonecode = :ZONECODE)"
            clsVarParameter = New clsVariable() With {.nameColumn = "ZONECODE", _
                                                      .valueColumn = strZoneCode}
            colVarParameter.Add(clsVarParameter)
        End If
        If Not (strBranchCode = "0") Then
            strSql = strSql & " and ( cbs_branchcode = :BRANCHCODE or branchcode = :BRANCHCODE)"
            clsVarParameter = New clsVariable() With {.nameColumn = "BRANCHCODE", _
                                                      .valueColumn = strBranchCode}
            colVarParameter.Add(clsVarParameter)
        End If

        If Not (strStartDate = "" AndAlso strEndDate = "") Then
            Dim dteStartDate As Date = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, strStartDate) & " 00:00:00"
            Dim dteEndDate As Date = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, strEndDate) & " 23:59:59"
            strDateStart = dteStartDate.ToShortDateString
            strDateEnd = dteEndDate.ToShortDateString

            strSql = strSql & " and ((cbs_tran_date >= :STARTDATE and cbs_tran_date <= :ENDDATE)"
            strSql = strSql & " or (transdate >= :STARTDATE and transdate <= :ENDDATE))"

            clsVarParameter = New clsVariable() With {.nameColumn = "STARTDATE", _
                                                      .valueColumn = dteStartDate}
            colVarParameter.Add(clsVarParameter)

            clsVarParameter = New clsVariable() With {.nameColumn = "ENDDATE", _
                                                      .valueColumn = dteEndDate}
            colVarParameter.Add(clsVarParameter)
            If (strPrmCondition = "") Then
                strPrmCondition = strPrmCondition & String.Format(Resources.resPrintRptMaster.cdtDate, dteStartDate & "   ถึง   " & dteEndDate)
            Else
                strPrmCondition = strPrmCondition & "   ;   " & String.Format(Resources.resPrintRptMaster.cdtDate, dteStartDate & "   ถึง   " & dteEndDate)
            End If

        Else
            If (strPrmCondition = "") Then
                strPrmCondition = strPrmCondition & String.Format(Resources.resPrintRptMaster.cdtDate, "-")
            Else
                strPrmCondition = strPrmCondition & "   ;   " & String.Format(Resources.resPrintRptMaster.cdtDate, "-")
            End If
        End If

        cls.rptAMLO101_CBS(re, strSql, ds, colVarParameter)
        re.prmStartDate.Value = strDateStart
        re.prmEndDate.Value = strDateEnd

        ReportViewer1.Report = re

    End Sub

    Private Sub printRptAmlo102_Map_CBS()
        Dim ds As New dsAmloReport
        Dim cls As New clsConnToDbByReport
        Dim re As Amlo1_02_Map_CBS = New Amlo1_02_Map_CBS
        Dim colVarParameter As Collection = New Collection
        Dim strPrmCondition As String = ""
        Dim clsVarParameter As clsVariable
        Dim clsFormatDate As New clsDateFormat
        Dim strDateStart As String = ""
        Dim strDateEnd As String = ""
        UserRight = Session("clsMenuRight")

        strSql = "select * from V_AMLO102_CBS t where 1=1 "

        If Not (strDeptCode = "0") Then
            strSql = strSql & " and ( cbs_deptcode = :DEPTCODE or deptcode = :DEPTCODE)"
            clsVarParameter = New clsVariable() With {.nameColumn = "DEPTCODE", _
                                                      .valueColumn = strDeptCode}
            colVarParameter.Add(clsVarParameter)
        End If
        If Not (strZoneCode = "0") Then
            strSql = strSql & " and ( cbs_zonecode = :ZONECODE or zonecode = :ZONECODE)"
            clsVarParameter = New clsVariable() With {.nameColumn = "ZONECODE", _
                                                      .valueColumn = strZoneCode}
            colVarParameter.Add(clsVarParameter)
        End If
        If Not (strBranchCode = "0") Then
            strSql = strSql & " and ( cbs_branchcode = :BRANCHCODE or branchcode = :BRANCHCODE)"
            clsVarParameter = New clsVariable() With {.nameColumn = "BRANCHCODE", _
                                                      .valueColumn = strBranchCode}
            colVarParameter.Add(clsVarParameter)
        End If

        If Not (strStartDate = "" AndAlso strEndDate = "") Then
            Dim dteStartDate As Date = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, strStartDate) & " 00:00:00"
            Dim dteEndDate As Date = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, strEndDate) & " 23:59:59"
            strDateStart = dteStartDate.ToShortDateString
            strDateEnd = dteEndDate.ToShortDateString

            strSql = strSql & " and ((cbs_transdate >= :STARTDATE and cbs_transdate <= :ENDDATE)"
            strSql = strSql & " or (transdate >= :STARTDATE and transdate <= :ENDDATE))"

            clsVarParameter = New clsVariable() With {.nameColumn = "STARTDATE", _
                                                      .valueColumn = dteStartDate}
            colVarParameter.Add(clsVarParameter)

            clsVarParameter = New clsVariable() With {.nameColumn = "ENDDATE", _
                                                      .valueColumn = dteEndDate}
            colVarParameter.Add(clsVarParameter)
            If (strPrmCondition = "") Then
                strPrmCondition = strPrmCondition & String.Format(Resources.resPrintRptMaster.cdtDate, dteStartDate & "   ถึง   " & dteEndDate)
            Else
                strPrmCondition = strPrmCondition & "   ;   " & String.Format(Resources.resPrintRptMaster.cdtDate, dteStartDate & "   ถึง   " & dteEndDate)
            End If

        Else
            If (strPrmCondition = "") Then
                strPrmCondition = strPrmCondition & String.Format(Resources.resPrintRptMaster.cdtDate, "-")
            Else
                strPrmCondition = strPrmCondition & "   ;   " & String.Format(Resources.resPrintRptMaster.cdtDate, "-")
            End If
        End If

        cls.rptAMLO102_CBS(re, strSql, ds, colVarParameter)
        re.prmStartDate.Value = strDateStart
        re.prmEndDate.Value = strDateEnd

        ReportViewer1.Report = re



    End Sub

    Private Function convertStringEncode(ByVal strCon As String) As String
        Dim rtStr As String = ""
        Try
            rtStr = Encoding.UTF8.GetString(Encoding.GetEncoding("tis-620").GetBytes(strCon))
        Catch ex As Exception
            Throw ex
        End Try
        Return rtStr
    End Function
End Class
